package com.lzy.algorithm;

import java.util.HashSet;
import java.util.Set;

public class 无重复字符的最长子串 {

    public static void main(String[] args) {
        String str = "abcedfcbadeacea";
        System.out.println(getStr(str));
    }


    public static int getStr(String str) {

        int left = 0;
        int right = 0;
        int rk = 0;
        int anc = 0;
        int length = str.length();
        Set<Character> set = new HashSet<>();

        for (int i = 0; i < length; i++) {
            if (i != 0) {
                set.remove(str.charAt(i - 1));
            }
            while (rk < length && !set.contains(str.charAt(rk))) {
                set.add(str.charAt(rk));
                rk++;
            }
            if (rk - i > anc) {
                left = i;
                right = rk - 1;
            }
            anc = Math.max(anc, rk - i);
        }
        System.out.println(left + "\t" + right);
        return anc;
    }
}
